创建子进程类
普通的长时间操作界面容易卡死, 体验非常不好,为了提升体验, 所以将需要较长时间响应的事件都放到了子线程中执行, 这样主线程就不会出现假死的状态
方法1(继承QThread):
.h [头文件]
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
: public QThread
class Generate {
Q_OBJECT:
public(QObject* parent = nullptr);// 直接使用qt的类生成,这里没有默认的空指针,new的时候会报错,需要设置成nullptr
explicit Generate
:
protectedvoid run(); // 重写run函数
:
signalsvoid curNumber(int num);// 自定义信号, 传递数据
void curimg(QImage* img); // 传递图片, 如果是自定义的结构, 需要注册类型:qRegisterMetaType<int>("int") [int根据自己的类型设置];
:
public slots};
#endif // MYTHREAD_H
.c[源文件]
"mythread.h"
#include "qdebug.h"
#include
::Generate(QObject* parent) : QThread(parent)
Generate{
}
void Generate::run()
{
qDebug() << "current Thread id is: " << QThread::currentThread();
int num = 0;
while (1)
{
curNumber(num++);
emit if (num == 999)
{
qDebug() << " sub Thread is finished...";
break;
}
::usleep(1);
QThread}
}
mainwindow.c主线程
"qtwidgetsapplication_thread.h"
#include "MyCreate.h"
#include
<qdebug.h>
#include
::QtWidgetsApplication_Thread(QWidget *parent)
QtWidgetsApplication_Thread: QMainWindow(parent)
{
.setupUi(this);
uiqDebug() << "main thread is : " << QThread::currentThread();
// 将刚才的创建的类实例化
* subThread_A = new Generate ;
Generate
// 绑定信号和槽
connect(subThread_A, &Generate ::curNumber, this, [=](int num) {
.label->setNum(num);
ui});
// 启动线程
connect(ui.pushButton, &QPushButton::clicked, this, [=]() {
->start();
subThread_A});
}
方法二(moveToThread):
这种方法相对来说更加灵活
打开创建类的头文件
MyThread.h
修改
MyThread(QObject *parent);
—–>MyThread(QObject *parent=nullptr
);, 不修改在new的时候会报错 [类 不存在默认构造函数]
修改完成后如下
<QObject>
#include
class MyThread : public QObject
{
Q_OBJECT
public:
MyThread(QObject *parent=nullptr);
~MyThread();
// create working function to do sth what you want
void working(); //线程中执行的代码写到working中,这个函数名随意
:
signals// create a signal to kick the thread
void signal_hello(int count); // 设置一个触发信号, count为传递的变量, 将这个变量用来讲子线程和主线程之间进行传递数据.
};
打开创建类的源文件MyThread.cpp
将刚才创建的头文件进行实现 这个程序的作用主要是将子线程中的变量发送到主线程进行显示
"MyThread.h"
#include
"qdebug.h"
#include "qthread.h"
#include
::MyThread(QObject *parent)
MyThread: QObject(parent)
{}
::~MyThread()
MyThread{}
void MyThread::working()
{
int count = 0;
while (1) {
// kick the signal, and post the data [count] by this signal
signal_hello(++count); // 将信号发送, 并将数据发送到主线程
emit if (count == 999) {
qDebug() << "sub thread is stoped:";
break;
}
qDebug() << "current thread is:" << QThread::currentThread;
}
}
进入到主线程进行实现mainwindow.cpp
* work = new MyThread;
MyThread* thread_c = new QThread;
QThread
->moveToThread(thread_c); // 放到新线程中
work->start(); // 启动线程
thread_c
connect(ui.pushButton, &QPushButton::clicked, work, &MyThread::working);// 按钮点击信号
connect(work, &MyThread::signal_hello, this, [=](int count) {
.label->setNum(count);
ui}); // 界面刷新信号